package com.hospital.yygh.user.controller.api;

import com.hospital.yygh.common.result.Result;
import com.hospital.yygh.common.utils.AuthContextHolder;
import com.hospital.yygh.model.user.UserInfo;
import com.hospital.yygh.user.service.UserInfoService;
import com.hospital.yygh.vo.user.LoginVo;
import com.hospital.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 用户接口
 */
@Api(tags = "用户接口")
@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 用户手机号登录接口（容联云）
     *
     * @param loginVo
     * @return
     */
    @ApiOperation("用户手机号登录接口")
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo) {
        // HttpServletRequest request
        //loginVo.setIp(IpUtil.getIpAddr(request));
        Map<String, Object> login = userInfoService.loginUser(loginVo);
        return Result.ok(login);
    }

    // 取验证码的时候，忘了加application.properties的redis配置（要加上），redis有值，但是取出的key-value是null

    /**
     * 用户QQ邮箱登录接口（我选择这个）
     *
     * @param loginVo
     * @return
     */
    @ApiOperation("用户QQ邮箱登录接口")
    @PostMapping("loginEmail")
    public Result loginEmail(@RequestBody LoginVo loginVo) {
        // HttpServletRequest request
        //loginVo.setIp(IpUtil.getIpAddr(request));
        log.info("进入到loginEmail。。。");
        Map<String, Object> login = userInfoService.loginEmail(loginVo);
        return Result.ok(login);
    }

    /**
     * 用户认证接口
     * @param userAuthVo
     * @param request
     * @return
     */
    @ApiOperation("用户认证接口")
    @PostMapping("auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        // 传递两个参数 1、用户id  2、认证数据的vo对象
        userInfoService.userAuth(AuthContextHolder.getUserId(request), userAuthVo);
        return Result.ok();
    }

    /**
     * 获取用户id信息接口
     * @param request
     * @return
     */
    @ApiOperation("获取用户id信息接口")
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }



}
